package com.ltx.controller;

import com.ltx.pojo.Card;
import com.ltx.service.CardService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Card")
public class CardController {

    private final CardService cardService;
    public CardController(@Qualifier("cardServiceImpl") CardService cardService) {
        this.cardService = cardService;
    }



    //查询所有会员卡信息
    @RequestMapping("/allCard")
    public String  list(Model model){
        List<Card> cards = cardService.queryCardMapper();
        model.addAttribute("list",cards);
        for (Card card : cards) {
            System.out.println(card);
        }
        return "/allCard";
    }
    //跳转到办卡页面
    @RequestMapping("/toAddCard")
    public  String toAddCard(){
        return ("/addCard");
    }

    @RequestMapping("/addCard")
    public  String AddCard(int mId, String beginTime,int balance,String cardType) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(beginTime);
        Card card = new Card();
        card.setmId(mId);
        card.setBeginTime(parse);
        card.setCardType(cardType);
        card.setBalance(balance);
        card.setEndTime(parse,cardType);
        System.out.println(card);
        cardService.addCardMapper(card);
        return ("redirect:/Card/allCard");
    }
    //跳转到修改办卡信息界面
    @RequestMapping("/toUpdateCard")
    public  String toUpdateCard(int mId,Model model){
        Card card = cardService.queryByCardMapper(mId);
        model.addAttribute("cards",card);
        return ("/UpdateCard");
    }
    @RequestMapping("/UpdateCard")
    public  String UpdateCard(Card card){
        cardService.updateCardMapper(card);
        return ("redirect:/Card/allCard");
    }
    //删除会员
    @RequestMapping("/deleteCard")
    public String deleteCard(int mId)
    {
        cardService.deleteCardMapper(mId);
        return "redirect:/Card/allCard";
    }
    //根据会员id查询会员卡信息
    @RequestMapping("queryCard")
    public  String queryCard(int mId,Model model){
        Card card = cardService.queryByCardMapper(mId);
        List<Card> cards=new ArrayList<Card>();
        cards.add(card);
        if(card==null){
            cards=cardService.queryCardMapper();
            model.addAttribute("error","未查询到");
        }
        model.addAttribute("list",cards);
        return "allCard";
    }

}
